package com.ruoyi.platform.system.role.enums;

import com.ruoyi.base.enums.ExceptionEnum;
import com.ruoyi.base.exception.SysException;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * 角色
 *
 * @author ruoyi
 */
public enum RoleEnum {
    /**
     * 管理员
     */
    MANAGER(1, "管理员"),
    /**
     * 普通用户
     */
    NORMAL(2, "普通用户");

    @Getter
    private final int code;
    @Getter
    private final String roleName;

    RoleEnum(int code, String roleName) {
        this.code = code;
        this.roleName = roleName;
    }

    public static RoleEnum ofCode(Integer code) {
        return Arrays.stream(RoleEnum.values()).filter(x -> Objects.equals(x.getCode(), code)).findAny().orElse(null);
    }

    public static RoleEnum checkRoleCode(Integer code) {
        return Optional.ofNullable(ofCode(code)).orElseThrow(() -> new SysException(ExceptionEnum.ROLE_ERROR));
    }
}
